//
//  MKMapViewController.m
//  singForYou
//
//  Created by nicolas on 2017/3/7.
//  Copyright © 2017年 nicolas. All rights reserved.
//

#import "MKMapViewController.h"

//工具模块
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
//地图模块
#import <BaiduMapAPI_Map/BMKMapComponent.h>
//定位模块
#import <BaiduMapAPI_Location/BMKLocationComponent.h>

#import "MKMapView.h"


@interface MKMapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>

@property (nonatomic, strong) BMKMapView *mapView;

@property (nonatomic, strong) BMKLocationService *locationService;

@end

@implementation MKMapViewController

#pragma mark -View Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.tabBarController.tabBar.hidden = YES;
    
//    [self.navigationController setNavigationBarHidden:YES];
    
    BMKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    
    [self.view addSubview:mapView];
    
    self.mapView = mapView;
    
    [self.mapView setShowsUserLocation:YES];
    
    [self addLocationBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.mapView viewWillAppear];
    
    self.mapView.delegate = self;
    
    self.locationService.delegate = self;
    
    [self.locationService startUserLocationService];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.mapView viewWillAppear];
    
    self.mapView.delegate = nil;
    
    self.locationService.delegate = self;
    
}

- (void)viewDidLayoutSubviews {
    
    [super viewDidLayoutSubviews];
}

#pragma mark - PrepareUI
- (void)addLocationBtn {
    UIButton *ltBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height-90, 30, 30)];
    [ltBtn setBackgroundImage:[UIImage imageNamed:@"locationBtn_region"] forState:UIControlStateNormal];
    
    [ltBtn addTarget:self action:@selector(locationBtnAction) forControlEvents:UIControlEventTouchDown];
    
    [self.mapView addSubview:ltBtn];
}

#pragma mark - Action
- (void)locationBtnAction {
    
    [self.locationService startUserLocationService];
}

#pragma mark - BMKMapViewDelegate & BMKLocationServiceDelegate
//获取到用户位置
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    
    [self.mapView updateLocationData:userLocation];
    
    BMKCoordinateRegion region = BMKCoordinateRegionMake(userLocation.location.coordinate, BMKCoordinateSpanMake(0.01, 0.01));
    [self.mapView setRegion:region animated:YES];
    
    [self.locationService stopUserLocationService];
    
}

#pragma mark - Getter & Setter
- (BMKLocationService *)locationService {
    
    if (!_locationService) {
        _locationService = [[BMKLocationService alloc] init];
    }
    return _locationService;
}

@end
